<template>
    <div class="page-bottom-bar">
        <div class="flex-item check-all-select">
            <van-checkbox v-model="allSelectState">全选</van-checkbox>
        </div>
        <div class="flex-item tip-message">已选择 {{ $store.state.shop.selectItems.length }} 条数据</div>
        <div class="flex-item">
            <span @click="eventClickPredictOrderPrice" class="bar-button bar-button-orange">立即下单</span>
        </div>
        <!--<div class="flex-item">-->
        <!--    <span @click="eventClickShopAddOrder" class="bar-button bar-button-green">立即下单</span>-->
        <!--</div>-->
    </div>
</template>

<style lang="scss" scoped>
    @import "../../../libs/styles/bottom-fixed-bar.scss";
</style>

<script>
    import { Checkbox } from 'vant';

    export default {
        components: {
            [Checkbox.name]: Checkbox
        },
        computed: {
            allSelectState: {
                get() {
                    return this.$store.state.shop.status.allSelect;
                },
                set() {
                    var status = this.$store.state.shop.status.allSelect,
                        items = [];

                    if(!status) {
                        for(let i in this.$store.state.shop.carts) {
                            items.push(i);
                        }
                    }

                    this.$store.commit('shop/allSelectCartsItem', items);
                    this.$store.commit('shop/setElementValue', [
                        { key: 'status.allSelect', value: !status }
                    ]);
                }
            }
        },
        methods: {
            buildFormaDataset(mode) {
                var result = [];

                for(let index of this.$store.state.shop.selectItems) {
                    let x = this.$store.state.shop.carts[index];
                    // console.log(x);

                    if(mode = 'predict') {
                        result.push({
                            id: x.id,
                            garbageid: x.idList,
							danweiming: x.unit,
							number: x.number
                        });
                    } else {
                        result.push({
                            garbageid: x.idList,
							danweiming: x.unit,
							number: x.number
                        });
                    }
                }

                return result;
            },
            eventClickPredictOrderPrice() {
                if(this.$store.state.shop.selectItems.length == 0) {
                    this.$toast('请选择要预估的选项');
                    return;
                }

                var url = '/api/home/user/getprice',
                    params = {
                        ids: JSON.stringify(this.buildFormaDataset('predict')),
                        token: this.$store.state.options.user.token
                    };

                this.$util.postRequestInterface(url, params, (res) => {
                    var carts = JSON.parse(JSON.stringify(this.$store.state.shop.carts));
                    for(let x of res.data.garbagelist) {
                        for(let y of carts) {
                            if(x.id == y.id) {
                                y.price = x.price;
                            }
                        }
                    }

                    this.$store.commit('shop/setElementValue', [
                        {key: 'orderPrice', value: res.data.price}
                    ]);
                    this.$store.commit('shop/resetShopCartsDataset', carts);
                    this.$router.push('/shop/order-predict-price');
                });
            },
            eventClickShopAddOrder() {
                if(this.$store.state.shop.selectItems.length == 0) {
                    this.$toast('请选择要下单的选项');
                    return;
                }

                var url = '/api/home/order/shopadd',
                    params = {
                        stock_id: 1,
                        garbagelist: JSON.stringify(this.buildFormaDataset()),
                        is_tracing: (this.$store.state.shop.selectMode == 'scan') ? 2 : 1,
                        order_sn: (this.$store.state.shop.selectMode == 'scan') ? this.$store.state.shop.order : '',
                        is_protect: 0,
                        token: this.$store.state.options.user.token
                    };

                this.$util.postRequestInterface(url, params, (res) => {
                    this.$store.commit('shop/resetShopCartsDataset', []);
                    this.$router.push({
                        path: '/shop/withdraw', query: {
                            ordernumber: res.data.ordernumber
                        }
                    });
                });
            }
        }
    }
</script>
